<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Message\Material;

use App\Http\Models\Chat;
use App\Http\Models\Record;
use App\Http\Models\PayRequest;
use App\Http\Models\WithdrawRequest;
use App\Http\Models\User;

class ChatController extends Controller
{

    public function load(Request $request)
    {
        $from_id = $request->from_id;
        $data = Chat::where(['to_id'=>$from_id,'is_read'=>0])->get();
        Chat::where(['to_id'=>$from_id,'is_read'=>0])->update(['is_read'=>1]);
        return response()->json(['status' => '200', 'data'=>$data]);
    }

    public function save(Request $request)
    {
        $data = $request->all();
        $data['add_time'] = time();
        $chat = Chat::create($data);
        if ($chat)
            return response()->json(['status' => '200', 'msg'=>'发送成功']);
        return response()->json(['status' => '500', 'msg'=>'发送失败']);
    }

    public function unRead(Request $request)
    {
        $from_id = $request->from_id;
        $data = Chat::where(['to_id'=>$from_id,'is_read'=>0])->count();
        return response()->json(['status' => '200', 'data'=>$data]);
    }

}